/**
 * Created by yihua on 2017/2/26.
 */
var pictureUrl = '';
$(document).ready(function () {
    editCategory();
    $("#edit_confirm").unbind('click').click(function () {
        var options = {
            url: '/api/goodscategory/editgoodscategory',
            beforeSubmit: validate,  // pre-submit callback
            success: editshowResponse,  // post-submit callback
            dataType: 'json',
            type: 'get',
            data: {
                'cat_id': $("#edit_category_id").val(),
                'img': pictureUrl
            }
        };
        $("#editCatgoryForm").ajaxSubmit(options)
    });

});

function editshowResponse(responseText, statusText, xhr, $form) {
    console.log(responseText);
    if (0 !== responseText.errno) {
        toastr.error(responseText.errmsg, '', {positionClass: 'toast-bottom-center'});
    } else {
        toastr.success('成功', '', {positionClass: 'toast-bottom-center'});
        $('#editModal').modal("hide");
        window.location.reload();
    }
}


function editCategory() {
    var chakanherf = new Array();
    $(".catEdit").each(function () {
        $(this).click(function () {
            $('#editModal').modal("show");
            //console.log($(this).parent().parent().find("td").eq(1).text())
            var catInfo = $(this).parent().parent().find("td");
            var cat_id = catInfo.eq(0).text();
            var category_name = catInfo.eq(1).text();
            var keywords = catInfo.eq(2).text();
            var cat_desc = catInfo.eq(3).text();
            var parent_id = catInfo.eq(4).text();

            chakanherf.push($(this).parent().parent().find("a")[0].href);
            $("#edit_category_id").val(cat_id);
            $("#edit_cat_name").val(category_name);
            $("#edit_keywords").val(keywords);
            $("#edit_cat_desc").val(cat_desc);
            initCategory('#edit_parent_id', '#editModal');
            $("#edit_parent_id").val(parent_id).trigger("change");
            //console.log(chakanherf);
            //$('#editModal').modal("hide");
            pictureUrl = $(this).parent().parent().find("a")[0].href;
            console.log('++++', pictureUrl)
            test($(this).parent().parent().find("a")[0].href);
        });
    });

}
function test(initialImg) {
    var content = '<img src=' + initialImg + ' style="width:130px">';
    $("#editimg").fileinput({
        uploadUrl: "/common/uploadimage",
        overwriteInitial: true,
        showUpload: false,
        uploadAsync: true,
        maxFileSize: 1500,
        showClose: false,
        showCaption: false,
        browseLabel: '',
        removeLabel: '',
        browseIcon: '<i class="glyphicon glyphicon-folder-open"></i>',
        removeIcon: '<i class="glyphicon glyphicon-remove"></i>',
        removeTitle: 'Cancel or reset changes',
        elErrorContainer: '#kv-avatar-errors-1',
        msgErrorClass: 'alert alert-block alert-danger',
        defaultPreviewContent: content,
        layoutTemplates: {main2: '{preview} ' + ' {remove} {browse}'},
        allowedFileExtensions: ["jpg", "png", "gif"]
    }).on('fileuploaded', function (event, data, id, index) {
        var ret = data.response;
        if (0 != ret.errno) {
            toastr.error('头像上传失败', '', {positionClass: 'toast-bottom-center'});
        } else {
            pictureUrl = ret.data.url;
            console.log(pictureUrl);
        }

    }).on("filebatchselected", function (event, files) {
        // trigger upload method immediately after files are selected
        $("#editimg").fileinput("upload");
    });

}